<!--商城列表-->
<template>
  <div class="bg bg1">
    <template v-if="isShow">

      <div class="row acenter">
        <text class="font26 text2">{{T('数量')}}</text>

        <div class="all"></div>
        <div class="but bg2" @click="jianClick">
          <text :class="['font44', number == 1 ? 'text3' : 'text2']">{{'-'}}</text>
        </div>
        <div class="value bg2">
          <text class="font30 text1">{{number}}</text>
        </div>
        <div class="but bg2" @click="jiaClick">
          <text :class="['font44', number == 12 ? 'text3' : 'text2']">{{'+'}}</text>
        </div>
      </div>

      <div class="height60"></div>
      <div class="row acenter">
        <text class="font26 text2 all">{{T('单价')}}</text>
        <text class="font22 text2">{{isVtnEquity ? T('含提现权益') : T('免提现权益')}}</text>
        <text class="font26 text1">{{' '+price+' VTN/'+T('月')}}</text>
      </div>

      <div class="height20"></div>
      <div class="row acenter">
        <text class="font26 text2 all">{{T('金额')}}</text>
        <text class="font26 text1">{{prices}}</text>
        <text class="font26 text1">{{''+' VTN'}}</text>
      </div>

      <div class="height20"></div>
      <div class="row acenter">
        <text class="font26 text2 all">{{T('折扣')}}</text>   
        <text class="font26 text1">{{discounts}}</text>  
        <text class="font26 text1">{{''+' VTN'}}</text>
      </div>

      <div class="height60"></div>
      <text class="font36 text1 tRight">{{T('应付金额')+'：'+pay+' VTN'}}</text>

      <template  v-if="warning > 0">
      <div class="height20"></div>
        <div class="row">
          <div class="all"></div>
          <text class="font26 warning-t1">{{warning == 1 ? T('VTN余额不足') : T('提现权益不足')}}</text>
          <text class="font26 main-t1" @click="vtnClick">{{'  '+T('去充值')}}</text>
        </div>
      </template>

      <div class="buy main1" @click="buyClcik">
        <text class="font30 text1">{{T('提交订单')}}</text>
      </div>
    </template>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.bg {
  flex: 1;
  padding-left: 40px;
  padding-top: 40px;
  padding-right: 40px;
}
.but {
  justify-content: center;
  align-items: center;
  width: 60px;
  height: 60px;
}
.value {
  justify-content: center;
  align-items: center;
  width: 120px;
  height: 60px;

  margin-left: 30px;
  margin-right: 30px;
}
.buy {
  justify-content: center;
  align-items: center;
  height: 70px;
  border-radius: 4px;

  margin-top: 120px;
}
</style>

<script>
import jnavigator from "@/_js/navigator";
import jmodal from "@/_js/modal";
import jconfig from "@/_js/config";
import jmath from "@/_js/math";
import jhttps from "@/_js/https";
import jbroadcast from "@/_js/broadcast";

const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {
  
  data() {
    return {
      T: T,
      Config:jconfig,
      isShow:false,

      number:1, //数量
      prices:0, //金额
      discounts:0, //折扣
      pay:0, //应付
      warning:0, //1余额不足 2权益不足
      
      user:undefined, //用户信息

      type:true, //true购买 false续费
      strategyId:'',
      isVtnEquity:false, //turn包喊提现权益
      price:0, //单价
      discount:0, //折扣

    };
  },

  mounted() {

    var param = weex.config;
    weexFunction.setTitle(param.isBuy ? T('立即购买') : T('续费'));

    this.strategyId = param.strategyId;
    this.httpUserInfo();
  },

  methods: {

    initData() {

      this.prices = jmath.cheng(this.price, this.number);
      this.discountss = jmath.cheng(this.discount, this.number);
      this.pay = jmath.jian(this.prices, this.discountss);

      if (Number(this.user.vtnAmount) < Number(this.pay)) {
        this.warning =  1;
      } else if (this.isVtnEquity && Number(this.user.vtnEquityAmount) < Number(this.pay)) {
        this.warning = 2;
      } else {
        this.warning = 0;
      }
    },
    
    /********************************* HTTP*/
    //http用户信息
    httpUserInfo() {

      var param = {};
      jhttps.postX("systemV2/queryMemberDetail", param,true,(data) => {
        if (data.status == 200) {
          this.user = data.data; 

          this.httpShopInfo();
        } else {
          jmodal.toast(data.msg);
        }
      });
    },

    //http商品详情
    httpShopInfo() {

      var param = {'strategyId':this.strategyId};
      jhttps.post("workShop/queryShopStrategyDetail", param,true,(data) => {
        if (data.status == 200) {
          var data = data.data; 

          this.isVtnEquity = data.isVtnEquity;
          if (data.origPrice == 0) {
            this.price = data.price;
            this.discount = 0;
          } else {
            this.price = data.origPrice;
            this.discount = jmath.jian(data.origPrice, data.price);
          }
          this.initData();
          this.isShow = true;
        } else {
          jmodal.toast(data.msg);
        }
      });
    },
    
    //http购买策略
    httpBuy() {

      jmodal.showLoad();
       var param = {'strategyId':this.strategyId, 
                    'monthNumber':this.number+''};
      jhttps.postX("tradeV2/buyShopStrategy", param,true,(data) => {
        if (data.status == 200) {

          weexFunction.setShowTabBar(1);
          weexFunction.postNotificationName('StrategyShow', {'index':0});
          setTimeout(() => {
          jmodal.hideLoad();
          jmodal.toast(T('购买成功'));
            jbroadcast.send('UpShow', {'index':0});
            jnavigator.back(10);
          }, 500);
        } else {
        jmodal.hideLoad();
          jmodal.toast(data.msg);
        }
      });
    },
    /********************************* 点击触发*/
    //减
    jianClick() {
      if (this.number == 1) return;
      this.number -= 1; 
      this.initData();
    },

    //加
    jiaClick() {
      if (this.number == 12) return;
      this.number += 1;  
      this.initData();
    },

    //充值
    vtnClick() {
      var nav = {'navColor':'#0c0c0c'};
      var param = {'wallet':'consumeWallet'};
      jnavigator.open("", "wallet/WalletBuy", param, nav);
    },
    
    //购买
    buyClcik() {

      if (this.warning == 1) {
          jmodal.toast(T('VTN余额不足'));
          return;
      }

      if (this.warning == 2) {
          jmodal.toast(T('提现权益不足'));
          return;
      } 

      var parm = {'title':T("支付确认"),
                  'message':T("您正在购买策略商城中的策略，请确认是否继续支付VTN。"),
                  'okBtn':T("确认"), cancelBtn:T("取消")};
      jmodal.confirm(parm, (bool)=>{
        if (bool == "true") {
          this.httpBuy();
        }
      });
    }
  },
};
</script>
